package com.yycx.module.bus.provider.handler.haiying.deposit;

import com.yycx.common.base.entity.EntityMap;
import com.yycx.common.base.utils.FlymeUtils;
import com.yycx.common.constants.CommonConstants;
import com.yycx.common.mybatis.interceptor.SaveInterceptor;
import com.yycx.common.mybatis.query.CriteriaSave;
import com.yycx.common.security.OpenHelper;
import com.yycx.common.utils.ApiAssert;
import com.yycx.common.utils.OrderNoUtil;
import com.yycx.common.utils.RedisUtils;
import com.yycx.module.bus.client.entity.BusDeposit;
import com.yycx.module.bus.client.entity.BusPayment;
import com.yycx.module.bus.client.enums.AmountTypeEnum;
import com.yycx.module.bus.provider.handler.haiying.BusinessUtils;
import com.yycx.module.bus.provider.service.BusDepositService;
import com.yycx.module.bus.provider.service.BusPaymentService;
import com.yycx.module.bus.provider.service.hy.HyOrderDetailService;
import com.yycx.module.bus.provider.service.hy.HyOrderService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.math.BigDecimal;

/**
 * 退质押金保存扩展器
 *
 * @author zyf
 */
@Component
public class RefundDepositPaymentSaveHandler implements SaveInterceptor<BusPayment> {


    @Resource
    private BusinessUtils businessUtils;
    @Resource
    private BusDepositService depositService;

    @Resource
    public RedisUtils redisUtils;

    @Override
    public void prepare(CriteriaSave cs, EntityMap entityMap, BusPayment payment) {
        Long userId = OpenHelper.getUserId();
        Long companyId = OpenHelper.getCompanyId();
        Long depositId = cs.getLong("depositId");
        String refundAmount = cs.getParams("refundAmount");
        String takeBankName = cs.getParams("takeBankName");
        String takeBankNo = cs.getParams("takeBankNo");
        BusDeposit deposit = depositService.getById(depositId);
        ApiAssert.isNotEmpty("退款对象不存在", deposit);
        //支付金额
        BigDecimal amount = deposit.getPayAmount();
        //扣款金额
        BigDecimal deductAmount = amount.subtract(new BigDecimal(refundAmount));
        payment.setOrganizationId(OpenHelper.getOrganizationId());
        payment.setCompanyId(OpenHelper.getCompanyId());
        payment.setPrintTag(CommonConstants.DISABLED);
        payment.setOrderId(depositId.toString());
        payment.setUserId(OpenHelper.getUserId());
        payment.setNickName(OpenHelper.getNickName());
        payment.setTitle("退保证金支付单");
        payment.setSaleManId(deposit.getSaleManId());
        payment.setAmountType(AmountTypeEnum.RefundDeposit.getCode());
        String paymentNo = businessUtils.getPaymentNo(companyId, userId);
        payment.setPaymentNo(paymentNo);
        payment.setBankName(takeBankName);
        payment.setBankNo(takeBankNo);
        payment.setAmount(new BigDecimal(refundAmount));
        payment.setReturnAmount(deductAmount);
        payment.setPaymentState(CommonConstants.INT_0);
    }

    @Override
    public void complete(CriteriaSave cs, EntityMap params, BusPayment entity) {
        Long depositId = cs.getLong("depositId");
        //关联退款ID
        depositService.updateRefundId(entity.getPaymentId(), depositId);
    }
}
